<?php

FLEA::loadClass('Common_Model');

class Model_Users extends Common_Model {

	private $_tableUser;
	
	function __construct() {
		$this->_tableUser = FLEA::getSingleton('Table_Users');
	}

	function login($data) {	
		// 验证用户名和密码是否正确
		
		$data['password'] = substr($data['password'], 3) . substr($data['password'], 0, 3) . 'qgzxs';
		
		$user = $this->_tableUser->findByUsername($data['username']);
		if (!$user || !$this->_tableUser->checkPassword($data['password'], $user[$this->_tableUser->passwordField])) {
			return '用户名或密码错误';
		}
	
		// 获取用户角色信息
		$roles = $this->_tableUser->fetchRoles($user);
		$rbac = FLEA::getSingleton('FLEA_Com_RBAC');
	
		//写进session的信息
		$sessionUser = array(
			'USERID' => $user[$this->_tableUser->primaryKey],
			'USERNAME' => $user[$this->_tableUser->usernameField],
		);
	
		dump($roles);
		$rbac->setUser($sessionUser, $roles);
		
		// 登录成功
		dump($_SESSION);
		return null;
	}
	
	function  register($data) {
		$userInfo = array(
			'username' => $data['username'],
			'password' => $data['password'],
			'confirmPassword' => $data['confirmPassword'],
			'email' => $data['email'],
			'profile' => array(
				'stuNum' => $data['stuNum'],
				'trueName' => $data['trueName'],
				'userType' => $data['userType'],
				'sex' => $data['sex'],
				'poor' => $data['poor'],
				'bankCardNum' => $data['bankCardNum'],
				'politics' => $data['politics'],
				'tel' => $data['tel'],
				'room' => $data['room'],
				'college' => $data['college'],
				'department' => $data['department'],
				'major' => $data['major'],
				'province' => $data['province'],
			),
		);
		$userInfo['password'] = substr($userInfo['password'], 3) . substr($userInfo['password'], 0, 3) . 'qgzxs';
		dump($userInfo);
		//$data['profile']['stuNum'] = $data['stuNum'];
		$result = array();
		if ( existsUsername($userInfo['username']) ) {$result[] = '该用户名已经存在!';}
		//if ( $this->existsStudentNumber($data['stuNum']) ) {$result[] = '该学号已经存在!';}
		//if ( existsEmail($userInfo['email']) ) {$result[] = '该邮箱已经注册过!';}
		//if ( count($result) > 0 ) {return $result;}

		//校验没做
		//校验应该包括网页前台的js validator和Model层的一些特殊校验方法
		
		//创建用户
		$insertId = $this->_tableUser->create($userInfo);
		
		//注册后进行登陆
		$userInfo['password'] = $userInfo['confirmPassword'];
		dump($userInfo['password']);
		$this->login($userInfo);
		dump($_SESSION);

		return $result;
	}
	
	function  modify() {
		
	}
	
	function getUserById($userId) {
		$_tableUser = FLEA::getSingleton('Table_Users');
		//$_tableUser->enableLink('teacher');
		$userData = $this->_tableUser->findByUserId($userId);
		return $userData;
	}
	
	function getForm($type, $data = null) {
		$form = $this->form($type, $data);
		return $form;
	}
	
	//以下几个方法作用是生成form元素的
	var $_ui = null;
	function form($type = 'login', $data = null) {
		$this->_ui = FLEA::initWebControls();
		switch ($type) {
			case 'login' : return $this->loginForm($data); break;
			case 'register' : return $this->registerForm($data); break;
			case 'modify' : return $this->modifyForm($data); break;
		}
	}

	function loginForm($data) {
		$form = array(
			'username' => $this->_ui->control('Textbox', 'username',
				array(
					'value' => $data['username'],
					'maxlength' => 18
				),
				true
			),
			'password' => $this->_ui->control('Password', 'password',
   				array(
       		 		'maxlength' => 18
    			),
				true
			)
		);
		return $form;
	}

	function registerForm($data) {
		$form = array(
			'username' => $this->_ui->control('Textbox', 'username',
				array(
					'maxlength' => 18
				),
				true
			),
			'password' => $this->_ui->control('Password', 'password',
   				array(
					'id' => 'pwd',
       		 		'maxlength' => 18
    			),
				true
			),
			'confirmPassword' => $this->_ui->control('Password', 'confirmPassword',
   				array(		
					'id' => 'confirmPwd',
       		 		'maxlength' => 18
    			),
				true
			),
			'trueName' => $this->_ui->control('Textbox', 'trueName',
				array(
				),
				true
			),
			'politics' => $this->_ui->control('RadioGroup', 'politics',
				array(
					'items' => array('共产党员' => 0, '共青团员' => 1, '其它'=>2),
					'selected' => 1
				),
				true
			),
			'stuNum' => $this->_ui->control('Textbox', 'stuNum',
				array(				
				),
				true
			),
			'sex' => $this->_ui->control('RadioGroup', 'sex',
				array(
					'items' => array('男' => 0, '女' => 1),
					'selected' => 0
				),
				true
			),
			'province' => $this->_ui->control('DropdownList', 'province',
				array(
					'selected' => '广东',
					'items' => array('请选择' => '',
									  '北京' => '北京',
									  '上海' => '上海',
									  '天津' => '天津',
									  '重庆' => '重庆',
									  '河北' => '河北',
									  '山西' => '山西',
									  '辽宁' => '辽宁',
									  '吉林' => '吉林',
									  '黑龙江' => '黑龙江',
									  '江苏' => '江苏',
									  '浙江' => '浙江',
									  '安徽' => '安徽',
									  '福建' => '福建',
									  '江西' => '江西',
									  '山东' => '山东',
									  '河南' => '河南',
									  '湖北' => '湖北',
									  '湖南' => '湖南',
									  '广东' => '广东',
									  '海南' => '海南',
									  '四川' => '四川',
									  '贵州' => '贵州',
									  '云南' => '云南',
									  '陕西' => '陕西',
									  '甘肃' => '甘肃',
									  '青海' => '青海',
									  '内蒙古' => '内蒙古',
									  '广西' => '广西区',
									  '西藏' => '西藏',
									  '宁夏' => '宁夏',
									  '新疆' => '新疆',
									  '台湾' => '台湾',
									  '香港' => '香港',
									  '澳门' => '澳门',
									  '国外' => '国外',
									)
				),
				true
			),
			'poor' => $this->_ui->control('RadioGroup', 'poor',
				array(
					'items' => array('有绿卡' => 1, '无绿卡但有贫困证明'=>2, '均无' => 0),
					'selected' => 0
				),
				true
			),
			'userType' => $this->_ui->control('RadioGroup', 'userType',
				array(
					'items' => array('本科内招'=>0, '自筹研究生'=>1),
					'selected' => 0
				),
				true
			),
			'college' => $this->_ui->control('DropdownList', 'college',
				array(
					'onChange' => 'selectDepartment()',
					
					'items' => array('请选择：'=>'')
				),
				true
			),			
			'department' => $this->_ui->control('DropdownList', 'department',
				array(
					'onChange' => 'selectMajor()',
					'items' => array('请选择：'=>'')
				),
				true
			),
			'major' => $this->_ui->control('DropdownList', 'major',
				array(
					'items' => array('请选择：'=>'')
				),
				true
			),
			'room' => $this->_ui->control('Textbox', 'room',
				array(
				),
				true
			),
			'bankCardNum' => $this->_ui->control('Textbox', 'bankCardNum',
				array(
				),
				true
			),
			'tel' => $this->_ui->control('Textbox', 'tel',
				array(
				),
				true
			),
			'email' => $this->_ui->control('Textbox', 'email',
				array(	
				),
				true
			),
			'pic' => $this->_ui->control('FileUpload', 'pic',
				array(
				),
				true
			),
			'question' => $this->_ui->control('Textbox', 'question',
				array(
				),
				true
			),
			'answer' => $this->_ui->control('Textbox', 'answer',
				array(
				),
				true
			)
		);
		return $form;
	}

	function modifyForm($data) {
		$form = array(
			'user_id' => $this->_ui->control('Hidden', 'user_id',
				array(
					'value' => $data['user_id']
				),
				true
			),
			'province' => $this->_ui->control('DropdownList', 'province',
				array(
					'items' => array('请选择' => '',
								   	'北京' => '北京',
						              '上海' => '上海',
						              '天津' => '天津',
						              '重庆' => '重庆',
						              '河北' => '河北',
						              '山西' => '山西',
						              '辽宁' => '辽宁',
						              '吉林' => '吉林',
						              '黑龙江' => '黑龙江',
						              '江苏' => '江苏',
						              '浙江' => '浙江',
						              '安徽' => '安徽',
						              '福建' => '福建',
						              '江西' => '江西',
						              '山东' => '山东',
						              '河南' => '河南',
						              '湖北' => '湖北',
						              '湖南' => '湖南',
						              '广东' => '广东',
						              '海南' => '海南',
						              '四川' => '四川',
						              '贵州' => '贵州',
						              '云南' => '云南',
						              '陕西' => '陕西',
						              '甘肃' => '甘肃',
						              '青海' => '青海',
						              '内蒙古' => '内蒙古',
						              '广西' => '广西区',
						              '西藏' => '西藏',
						              '宁夏' => '宁夏',
						              '新疆' => '新疆',
						              '台湾' => '台湾',
						              '香港' => '香港',
						              '澳门' => '澳门',
						              '国外' => '国外',
									),
					'selected' => $data['province']
				),
				true
			),
			'sex' => $this->_ui->control('RadioGroup', 'sex',
				array(
					'items' => array('男' => 0, '女' => 1),
					'selected' => $data['sex']
				),
				true
			),
			'politics' => $this->_ui->control('RadioGroup', 'politics',
				array(
					'items' => array('共产党员' => 0, '共青团员' => 1, '其它'=>2),
					'selected' => $data['politics']
				),
				true
			),
			'email' => $this->_ui->control('Textbox', 'email',
				array(
					'value' => $data['email']
				),
				true
			),
			'poor' => $this->_ui->control('RadioGroup', 'poor',
				array(
					'items' => array('有绿卡' => 1, '无绿卡但有贫困证明'=>2, '均无' => 0),
					'selected' => $data['poor']
				),
				true
			),
			'college' => $this->_ui->control('DropdownList', 'college',
				array(
					'onChange' => 'selectDepartment()',
					'items' => array($data['college']=>$data['college'])
				),
				true
			),
			'department' => $this->_ui->control('DropdownList', 'department',
				array(
					'selected' => $data['department'],
					'onChange' => 'selectMajor()',
					'items' => array($data['department']=>$data['department'])
				),
				true
			),			
			'major' => $this->_ui->control('DropdownList', 'major',
				array(
					'selected' => $data['major'],
					'items' => array($data['major']=>$data['major'])
				),
				true
			),
			'room' => $this->_ui->control('Textbox', 'room',
				array(
					'value' => $data['room']
				),
				true
			),
			'trueName' => $this->_ui->control('Textbox', 'trueName',
				array(
					'value' => $data['trueName']
				),
				true
			),
			'tel' => $this->_ui->control('Textbox', 'tel',
				array(
					'value' => $data['tel']
				),
				true
			),
			'bankCardNum' => $this->_ui->control('Textbox', 'bankCardNum',
				array(
					'value' => $data['bankCardNum']
				),
				true
			),
			'userType' => $this->_ui->control('RadioGroup', 'userType',
				array(
					'items' => array('本科内招'=>0, '自筹研究生'=>1),
					'selected' => $data['userType']
				),
				true
			),
			'pic' => $this->_ui->control('FileUpload', 'pic',
				array(
					'onChange' => 'view()'
				),
				true
			)
		);
		return $form;
	}

}